package com.fjwt.gz.proxy.ctrl.lite.homepage;

import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.core.model.security.GzUserDetails;
import com.fjwt.gz.db.entity.LiteHomepageEntity;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.LiteHomepageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * 首页接口类
 */
@Slf4j
@RestController
@RequestMapping("/api/liteHomePage")
public class LiteHomePageController extends CommonCtrl {

    @Autowired
    private LiteHomepageService homepageService;

    /**
     * 首页配置列表
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ApiRes getLiteHomepage() {
        Map map = getObject(Map.class);
        return ApiRes.page(homepageService.selectLiteHomepageList(getIPage(), map));
    }

    /**
     * 首页配置新增
     */
    @PostMapping("/save")
    public ApiRes liteHomepageSave() {
        GzUserDetails user = getCurrentUser();
        LiteHomepageEntity entity = getObject(LiteHomepageEntity.class);
        entity.setCreatedUid(user.getSysUserId());
        entity.setCreatedAt(new Date());
        int insert = homepageService.getBaseMapper().insert(entity);
        if (insert > 0) {
            return ApiRes.ok();
        } else {
            return ApiRes.customFail("操作失败！");
        }
    }

    /**
     * 首页配置修改
     */
    @PostMapping("/update")
    public ApiRes liteHomepageUpdate() {
        GzUserDetails user = getCurrentUser();
        LiteHomepageEntity entity = getObject(LiteHomepageEntity.class);
        LiteHomepageEntity result = new LiteHomepageEntity();
        BeanUtils.copyProperties(entity, result);
        result.setCreatedUid(user.getSysUserId());
        result.setUpdateAt(new Date());
        boolean b = homepageService.updateById(result);
        if (b) {
            return ApiRes.ok();
        } else {
            return ApiRes.customFail("操作失败！");
        }
    }

    /**
     * 首页配置新增
     */
    @PostMapping("/getInfo")
    public ApiRes liteHomepageGetInfo() {
        LiteHomepageEntity entity = getObject(LiteHomepageEntity.class);
        if (Objects.isNull(entity.getHomepageId())) {
            return ApiRes.customFail("请求参数缺失！");
        }
        LiteHomepageEntity object = homepageService.getById(entity.getHomepageId());
        return ApiRes.ok(object);
    }

    /**
     * 首页配置删除
     */
    @PostMapping("/remove")
    public ApiRes liteHomepageRemove() {
        LiteHomepageEntity entity = getObject(LiteHomepageEntity.class);
        if (Objects.isNull(entity.getHomepageId())) {
            return ApiRes.customFail("请求参数缺失！");
        }
        boolean b = homepageService.removeById(entity.getHomepageId());
        if (b) {
            return ApiRes.ok();
        }
        return ApiRes.customFail("操作失败！");
    }
}
